from datetime import datetime
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()


class User(db.Model):
    user_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
    username = db.Column(db.String(31), unique=True)
    password = db.Column(db.String(255))
    tel = db.Column(db.String(11))
    __tablename__ = 'tb_user'


class Category(db.Model):
    category_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
    fid = db.Column(db.Integer, db.ForeignKey('category.category_id'), nullable=True, default=None)
    alias = db.Column(db.String(32))
    name = db.Column(db.String(32), unique=True, nullable=False)
    keywords = db.Column(db.String(32), index=True, nullable=False)
    describe = db.Column(db.String(255), nullable=False)

    __tablename__ = 'category'

    def __repr__(self):
        return f'{self.name}'


class Article(db.Model):
    article_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
    title = db.Column(db.String(32), nullable=False)
    content = db.Column(db.Text, nullable=False)
    keywords = db.Column(db.String(32), index=True)
    describe = db.Column(db.String(255), nullable=False)
    titlepic = db.Column(db.String(255), nullable=False)
    visibility = db.Column(db.Boolean, nullable=False)
    time = db.Column(db.Date, default=datetime.now())
    category_id = db.Column(db.Integer, db.ForeignKey('category.category_id'))
    category = db.relationship('Category', backref='article')

    __tablename__ = 'article'

